<?php

namespace addon\lian\event;

use addon\lian\dict\LianDict;
use addon\lian\model\core\member\CoreLianLevelService;
use addon\lian\model\core\member\CoreLianMemberService;
use addon\lian\model\core\order\CoreLianRewardService;
use addon\lian\model\sqlmodel\LianMemberModel;
use addon\lian\model\sqlmodel\MemberModel;
use addon\lian\model\Config;
use think\facade\Log;

/**
 * 订单完成后
 */
class OrderComplete
{

    /**
     * 订单完成后
     */
    public function handle($order)
    {
        Log::info('==========================lianOrderComplete开始========================');
        $order_data = $order;
        $site_id = $order_data['site_id'];
        $member_id = $order_data['member_id'];

        $config = (new Config())->getLianBasicsConfig($site_id);
        $member = (new MemberModel())->where('site_id', $site_id)->where('member_id', $member_id)->findOrEmpty();
        $lian_member = (new LianMemberModel())->where('site_id', $site_id)->where('member_id', $member_id)->findOrEmpty();

        Log::info('==lianordercomplete==config==' . json_encode($config));
        $config = $config['data']['value'];
        if($config['up_way'] == LianDict::ORDERFINISH){
            if($lian_member->isEmpty()){

                (new CoreLianLevelService())->lianMemberUpgrade($site_id, $member); //成为链动会员

                (new CoreLianRewardService())->handleReward($site_id, $order_data); //计算佣金

                (new CoreLianMemberService())->bindChildMember($site_id, $member_id, $order_data);  //绑定链动关系

                

            }else{  
                (new CoreLianRewardService())->handleReward($site_id, $order_data); //计算佣金
            }
        }
        

        return true;
    }
}